/*
 * Copyright 2008 Wilfred Springer
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.agilejava.javaone;

import java.util.ArrayList;
import java.util.List;

public class Session {

    private SessionType type;

    private String title;

    private String id;

    private String detailsURL;

    private List<Presenter> presenters = new ArrayList<Presenter>(2);

    private String summary;

    public SessionType getType() {
        if (type == null) {
            if (id.startsWith("LAB")) return SessionType.HandsOnLab;
            if (id.startsWith("BOF")) return SessionType.BirdsOfAFeather;
            if (id.startsWith("TS")) return SessionType.TechnicalSession;
            if (id.startsWith("PAN")) return SessionType.PanelSession;
            return SessionType.Unknown;
        }
        return type;
    }

    public void setType(SessionType type) {
        this.type = type;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDetailsURL() {
        return detailsURL;
    }

    public void setDetailsURL(String detailsURL) {
        this.detailsURL = detailsURL;
    }

    public List<Presenter> getPresenters() {
        return presenters;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getSummary() {
        return summary;
    }

    public List<Element> getSummaryElements() {
        ArrayList<Element> results = new ArrayList<Element>();
        if (summary == null) return results;
        String[] lines = summary.split("\n");
        processTop(lines, 0, results);
        return results;
    }

    private void processTop(String[] lines, int position,
            ArrayList<Element> results) {
        StringBuilder builder = new StringBuilder();
        while (true) {
            if (position > lines.length - 1)
                break;
            if (lines[position].trim().length() == 0) {
                results.add(new Para(builder.toString()));
                builder.setLength(0);
            } else {
                char c = lines[position].trim().charAt(0);
                if ('•' == c || '-' == c) {
                    results.add(new Para(builder.toString()));
                    ItemizedList list = new ItemizedList();
                    position += processListItems(lines, position, list);
                    results.add(list);
                } else {
                    builder.append(lines[position].trim());
                    builder.append(' ');
                }
            }
            position++;
        }
        if (builder.length() > 0) {
            results.add(new Para(builder.toString()));
        }
    }

    private int processListItems(String[] lines, int position, ItemizedList list) {
        int processed = 0;
        while (true) {
            if (position > lines.length - 1)
                return processed;
            String line = lines[position].trim();
            if (line.length() == 0)
                return processed;
            char c = line.charAt(0);
            if ('•' == c || '-' == c) {
                list.getItems().add(new ListItem(line.substring(1).trim()));
                processed++;
                position++;
            } else {
                return processed;
            }
        }
    }

}
